<?php
add_action('wp_head','pluginname_ajaxurl');
function pluginname_ajaxurl() {
?>
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
<?php
}
add_action('wp_head', 'submit_coupon');
function submit_coupon(){ ?>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        //sugget store url
        var div_result ='<div id="result_url" style="position: absolute;background-color: red;z-index: 1000;width: 272px;margin-left: 14px;margin-top: 2px;">';
        //$('div#result').append(div_result + '</div>');
        $('input[name="store_name"]').keyup(function(){
            var url_input_send = $('input[name="store_name"]').val();
            if(url_input_send.length > 1){
                var loading_text = '<p><a style="color:#999999;margin-left:6px">Finding matching ...</a><p>';
                $('div#actor_result').html(loading_text);
                var data ={
                    action : 'sugget_url',
                    value_send : url_input_send
                };
                $.post(ajaxurl, data, function(result) {
                    //alert(result);
                    var arr_result = '';
                    if(result != ''){
                        arr_result = result.split(',');
                    }
                    var result_url='';
                    if(arr_result.length > 0){
                        for(i=0;i<arr_result.length;i++)
                        {
                            result_url += arr_result[i];
                        }
                        var xhtml = div_result + result_url + '</div>';
                        $('div#result_url').html(xhtml);
                        $('div#result_url').fadeIn(500);
                        
                        $('a#url_return').click(function(){
                           var value = this.text;
                           $('input[name="store_name"]').val(value);
                           $('div#result_url').fadeOut();
                        });
                    }else{
                        $('div#result_url').fadeOut();
                    }
                });
            }
            else
            {
                $('div#result_url').fadeOut();
            }
        });
        
        //drop down choose coupon type
        $('select[name="select_type"]').change(function(){
            var selectedValue = $(this).find(":selected").val();
            //alert(selectedValue);
            var div = $('div.type_code');
            if(selectedValue == 'code'){
                div.empty();
                 var html = '<input class="entercode" type="text" placeholder="Enter Coupon code"  autocomplete="off" name="coupon_code"/>';
                div.append(html);
            }
            if(selectedValue == 'promotion'){
                div.empty();
                var html = '<input class="entercode" type="text" placeholder="Offer Link"  autocomplete="off" name="offer_link"/>';
                html += '<div class="warming hiden offer-missing">*Enter Offer link</div>';
                div.append(html);
            }
            if(selectedValue == 'printable'){
                div.empty();
            }
        });        
    });
</script>
<?php }
add_action('wp_ajax_sugget_url', 'sugget_url_callback');
function sugget_url_callback(){
    $value_query = $_POST['value_send'];
    $args = array(
        'post_type' => 'store',
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => 'url_store_metadata',
                'value' => $value_query,
                'compare' => 'LIKE'
            )
        )
    );
    $query = new WP_Query($args);
    $store = $query->posts;
    //echo $value_query;
    foreach ($store as $st){
        echo "<p><a id='url_return' style='cursor:pointer' >".get_post_meta($st->ID,'url_store_metadata',true)."</a></p>";
    }
    die();
}
//add_action('wp_ajax_submit_cp1', '1submit_new_coupon_callback');
//function submit_new_coupon_callback(){
//    session_start();
//    global $wpdb,$table_prefix;
//    $value_received = $_POST['value_send'];
//    $store_name = $value_received[0];
//    $args = array(
//        'post_type' => 'store',
//        'post_status' => 'publish',
//        'meta_query' => array(
//            array(
//                'key' => 'url_store_metadata',
//                'value' => $store_name,
//                'compare' => '='
//            )
//        )
//    );
//    $query = new WP_Query($args);
//    $store = $query->posts;
//    $store_id ='';
//    foreach ($store as $st){
//        $store_id = $st->ID;
//    }    
//    $coupon_title = $value_received[1];
//    $code = $value_received[2];
//    $type_coupon = $value_received[3];
//    $ex_date = $value_received[4];
//    if($coupon_title != ''){
//        $new_store =  array(
//            'post_title' => $coupon_title,
//            'post_status'=>'draft',
//            'post_type' =>'coupon'
//        );
//        $id = wp_insert_post($new_store,true);
//        //destination_metadata
//        add_post_meta($id, store_coupon_metadata, $store_id);
//        add_post_meta($id, coupon_type_metadata, $type_coupon);
//        add_post_meta($id, expire_date_metadata, $ex_date);
//        if($type_coupon == 'code'){
//            add_post_meta($id, coupon_code_metadata, $code);
//        }
//        if($type_coupon =='promotion'){
//            add_post_meta($id, destination_metadata, $code);
//        }        
//        echo (home_url().'/sign-up') ;
//        die();
//    }else{
//        echo (home_url().'/submit-cp/?empt=true') ;
//        die();
//    }
//}
?>
